package homework;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

//定义一个类SchoolClass代表班级，
//该类中包含一个属性是一个HashSet的对象，
//用来存储该班级中所有的Student,
//并提供相应的addStudent(Student s)方法和removeStudent(String name)方法，
//表示添加一名学生和删除一名学生（删除学生时通过传学生姓名参数来确定）。
import java.util.Map;
public class SchoolClass {
	private HashSet<Student> classes = new HashSet<Student>();

	public SchoolClass(){}
	
	
	
	public HashSet<Student> getStudents() {
		return classes;
	}
	public void setStudents(HashSet<Student> students) {
		this.classes = students;
	}
	
	public void addStudent(Student s){
		classes.add(s);
	}
	
	public void removeStudent(String name){
		Iterator<Student> iterator = classes.iterator();
		while(iterator.hasNext()){
			Student student = iterator.next();
			if(student.getSname().equals(name)){
				iterator.remove();
			}
		}
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return this.classes.toString();
	}
	
//	为SchoolClass类写一个方法，统计每门课程的选课人数 Map account();
//	该方法返回一个Map，key代表课程，value代表人数。
	public Map<Course, Integer> accout(){
		Map<Course, Integer> map = new HashMap<Course, Integer>();
//		Set<Entry<Course, Integer>> set = map.entrySet();
//		ArrayList<Entry<Course, Integer>> alist = new ArrayList<Map.Entry<Course,Integer>>(set);
//		Iterator<Entry<Course, Integer>> iterator = alist.iterator();
		//ArrayList<Student> arrayList = new ArrayList<Student>(this.classes);
		//System.out.println(classes);
		//Iterator<Student> iterator = arrayList.iterator();
		Iterator<Student> iterator = classes.iterator();
		while(iterator.hasNext()){
			Student student = iterator.next();
			//System.out.println(student);
			//ArrayList<Course> arrayList2 = new ArrayList<Course>(student.getStudents());
			//System.out.println(arrayList2);
			//Iterator<Course> iterator2 = arrayList2.iterator();
			Iterator<Course> iterator2 = student.getStudents().iterator();
			System.out.println(iterator2);
			while(iterator2.hasNext()){
				Course course = iterator2.next();		
				System.out.println(course);
				if(map.containsKey(course)){
					int num =map.get(course)+1;
					map.put(course, num);
				}else{
					map.put(course, 1);
				}
				//num= num==null?1:num+1;
				//map.put(course, num);
			}
		}
		return map;
	}
	
}
